<?php
  class CronJobs
  {
    public static function parseSite($site_id = null)
    {
      set_time_limit(0);
      
      // delete old images
      $c = new Criteria();
      $c->addJoin(OfferPeer::ID, OfferImagePeer::OFFER_ID);
      $c->addAscendingOrderByColumn(OfferPeer::CREATED_AT);
      $c->setLimit(300);
      $images = OfferImagePeer::doSelect($c);
      
      foreach($images as $image)
      {
        if($offer = $image->getOffer())
        {
          if(!$offer->isActive())
          {
            $image->delete();
          }
        }
      }
      
      if(CronParserPeer::isRunning())
      {
        //echo "is running\n";
        return null;
      }
      
      $start_time = time();
      
      $sites = SitePeer::getAllActiveForParser();
      
      if(!$site_id)
      {
        $last_site_id = null;
        $next_site_id = null;
        if($last_cron_parser = CronParserPeer::getLastCronParser())
        {
          $last_site_id = $last_cron_parser->getSiteId();
        }
        
        if(!$last_site_id)
        {
          foreach($sites as $site)
          {
            $next_site_id = $site->getId();
            break;
          }
        }
        else
        {
          $next = false;  
          foreach($sites as $site)
          {
            if($last_site_id == $site->getId())
            {
              $next = true;
              continue;
            }
            if($next)
            {
              $next_site_id = $site->getId();
              break;
            }
          }
        }
        if(!$next_site_id)
        {
          foreach($sites as $site)
          {
            $next_site_id = $site->getId();
            break;
          }
        }
      }
      else
      {
        $next_site_id = $site_id;
      }
      //echo "site: ".$next_site_id."\n";
      
      $cron_parser = new CronParser();
      $cron_parser->setSiteId($next_site_id);
      $cron_parser->save();
      
      foreach($sites as $site)
      {
        if($site->getId() == $next_site_id)
        {
          $site_offers = array();
          switch($site->getId())
          {
            case 1: // fundeal
              //fundeal national
              $offers = SiteParser::parseFundeal('http://www.fundeal.ro/national/');
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  foreach(CityPeer::getAll() as $city)
                  {
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($city->getId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                  }
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
              
              $urls = $site->getSiteUrls();
              shuffle($urls);
              foreach($urls as $site_url)
              {
                $offers = SiteParser::parseFundeal($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 2:
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseZumzi($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 3:
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseGroupon($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              
              //groupon national
              $offers = SiteParser::parseGroupon('http://www.groupon.ro/deals/national');
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  foreach(CityPeer::getAll() as $city)
                  {
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($city->getId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                  }
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
              break;
            case 4: // goldendeals
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseGoldendeals($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 5: // kuponiada
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseKuponiada($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 6: // coolbuy
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseCoolbuy($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 7: // topdeals
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseTopdeals($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 8: // citydays
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseCitydays($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 9: // ideals
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseIdeals($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 10: // perfectdeals
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parsePerfectdeals($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 11: // getadeal.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseGetadeal($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 12: // reddeal.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseReddeal($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 13: // e-redus.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseEredus($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 14: // oferta.noi2.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseNoi2($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
            case 15: // profitam.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseProfitamro($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 16: // bestdealz.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseBestdealz($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 17: // qupon.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseQupon($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 18: // crazydeal.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseCrazydeal($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }

              //crazydeal national
              $offers = SiteParser::parseCrazydeal('http://www.crazydeal.ro/active/national/all');
              foreach($offers as $offer)
              {
                if($offer)
                {
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  foreach(CityPeer::getAll() as $city)
                  {
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($city->getId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                  }
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
              break;
              
            case 19: // fungroup.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseFungroup($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 20: // pandoria.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parsePandoria($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 21: // dealfever.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseDealfever($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 22: // reducerimaxime.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseReduceriMaxime($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              
              // oferte nationale
              $offers = SiteParser::parseReduceriMaxime('http://www.reducerimaxime.ro/nationale');
              // oferte online
              $offers = array_merge($offers, SiteParser::parseReduceriMaxime('http://www.reducerimaxime.ro/online'));
              $parsed_offers = array();
              foreach($offers as $offer)
              {
                if($offer && !in_array($offer->getId(), $parsed_offers))
                {
                  $parsed_offers[] = $offer->getId();
                  foreach($offer->getOfferCitys() as $offer_city)
                  {
                    try
                    {
                      $offer_city->delete();
                    }
                    catch(Exception $e)
                    {
                      // var_dump($e->getMessage());
                    }
                  }
                  foreach(CityPeer::getAll() as $city)
                  {
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($city->getId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                  }
                  $offer->setSiteId($site->getId());
                  $offer->setOnSite(true);
                  $offer->save(null, true);
                  
                  $site_offers[$site->getId()][$offer->getId()] = $offer;
                }
              }
              break;
              
            case 23: // therealdeal.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseTheRealDeal($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 24: // prodeal.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseProDeal($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 25: // waz.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseWaz($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    foreach(CityPeer::getAll() as $city)
                    {
                      $offer_city = new OfferCity();
                      $offer_city->setCityId($city->getId());
                      $offer_city->setOfferId($offer->getId());
                      $offer_city->save();
                    }
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
                
                break;
              }
              break;
              
            case 26: // pontomat.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parsePontomat($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 27: // promo-romania.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parsePromoRomania($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    foreach(CityPeer::getAll() as $city)
                    {
                      $offer_city = new OfferCity();
                      $offer_city->setCityId($city->getId());
                      $offer_city->setOfferId($offer->getId());
                      $offer_city->save();
                    }
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 28: // savemoney.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseSaveMoney($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 29: // localdeals.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseLocalDeals($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 30: // gogetit.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseGogetit($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 31: // wisebuy.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseWisebuy($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 32: // oferteiasi.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseOferteiasi($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 33: // ofertapret.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseOfertapret($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 34: // shopdeals.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseShopdeals($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 35: // ofertadelux.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseOfertadelux($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 36: // kupedia.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseKupedia($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 37: // dealmania.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseDealmania($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 38: // buyzilla.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseBuyzilla($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 39: // reduceri-oferte.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseReduceriOferte($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    foreach($offer->getOfferCitys() as $offer_city)
                    {
                      try
                      {
                        $offer_city->delete();
                      }
                      catch(Exception $e)
                      {
                        // var_dump($e->getMessage());
                      }
                    }
                    $offer_city = new OfferCity();
                    $offer_city->setCityId($site_url->getCityId());
                    $offer_city->setOfferId($offer->getId());
                    $offer_city->save();
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
              
            case 40: // easydeals.ro
              foreach($site->getSiteUrls() as $site_url)
              {
                $offers = SiteParser::parseEasyDeals($site_url->getUrl());
                foreach($offers as $offer)
                {
                  if($offer)
                  {
                    $offer->setSiteId($site->getId());
                    $offer->setOnSite(true);
                    $offer->save(null, true);
                    
                    $site_offers[$site->getId()][$offer->getId()] = $offer;
                  }
                }
              }
              break;
          }
          
          $current_offers = $site->getCurrentOffers(date('Y-m-d H:i:s'));
          foreach($current_offers as $key => $current_offer)
          {
            //echo '<br />Searching: '.$current_offer->getName()."<br />\n";
            $found = false;
            foreach($site_offers[$site->getId()] as $offer)
            {
              if($current_offer->getId() == $offer->getId())
              {
                $found = true;
              }
            }
            if(!$found)
            {
              if(!$current_offer->getPromo())
              {
                $current_offer->setEndTime(date('Y-m-d 00:00:00'));
                $current_offer->save();
                //echo 'Not found: '.$current_offer->getName()."<br />\n";
              }
            }
          }
          
          foreach($site_offers[$site->getId()] as $offer)
          {
            $offer->share();
          }
        }
      }

      $cron_parser->setDuration(time() - $start_time);
      $cron_parser->save();
    }
    
    public static function cleanSearch()
    {
      set_time_limit(0);
      
      ProjectConfiguration::registerZend();
      $index = OfferPeer::getLuceneIndex();
      $i = 0;
      $c = new Criteria();
      $c->add(OfferPeer::IN_SEARCH, 1);
      $offers = OfferPeer::doSelect($c);
      foreach($offers as $offer)
      {
        if(!$offer->isActive())
        {
          foreach($index->find('pk:'.$offer->getId()) as $hit)
          {
            $index->delete($hit->id);
            $i++;
          }
          $offer->setInSearch(false);
          $offer->save();
        }
      }
      
      $bodyHtml = 'Deleted '.$i.' records from search';
      
      /*
      $c = new Criteria();
      $c->add(OfferPeer::END_TIME, date("Y-m-d H:i:s", strtotime('-60 days')), Criteria::LESS_EQUAL);
      $c->addAscendingOrderByColumn(OfferPeer::CREATED_AT);
      $c->setLimit(100);
      $offers = OfferPeer::doSelect($c);
      foreach($offers as $offer)
      {
        $offer->delete();
      }
      */
      
      // $bodyHtml .= "\n".'Deleted '.count($offers).' old offers';
      
      $mailer = sfContext::getInstance()->getMailer();
      $message = $mailer->compose('website@reduceri-cadouri.ro', 'admin@reduceri-cadouri.ro', 'Cron CleanSearch');
      $message->setBody($bodyHtml,'text/html');
      $mailer->send($message);
    }
    
    public static function generateSitemap()
    {
      set_time_limit(0);
      
      sfContext::getInstance()->getConfiguration()->loadHelpers('Url');
      
      $sitemap = new DOMDocument('1.0', 'utf-8');
      $sitemap->formatOutput = true;
      $sitemap->encoding = "utf-8";
      $url_set = $sitemap->appendChild(new DOMElement('urlset'));
      $attribute_url_set = $url_set->setAttributeNode(new DOMAttr('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'));
      
      // homepage url
      $url = $url_set->appendChild(new DOMElement('url'));
      $location = $url->appendChild(new DOMElement('loc', url_for('@homepage', true)));
      $priority = $url->appendChild(new DOMElement('priority', '1'));
      $change_frequency = $url->appendChild(new DOMElement('changefreq', 'daily'));
      
      // cities
      $cities = CityPeer::getAll();
      foreach($cities as $city)
      {
        $url = $url_set->appendChild(new DOMElement('url'));
        $location = $url->appendChild(new DOMElement('loc', url_for('@city?city_name='.Utils::slugify($city->getName()), true)));
        $priority = $url->appendChild(new DOMElement('priority', '0.9'));
        $change_frequency = $url->appendChild(new DOMElement('changefreq', 'daily'));
      }

      // categories
      $categories = CategoryPeer::getAll();
      foreach($categories as $category)
      {
        $url = $url_set->appendChild(new DOMElement('url'));
        $location = $url->appendChild(new DOMElement('loc', url_for('@city?city_name=all&category_name='.Utils::slugify($category->getName()), true)));
        $priority = $url->appendChild(new DOMElement('priority', '0.8'));
        $change_frequency = $url->appendChild(new DOMElement('changefreq', 'daily'));
      }
      
      // offers
      $c = new Criteria();
      $c->add(OfferPeer::ON_SITE, 1);
      $c->add(OfferPeer::START_TIME, date("Y-m-d H:i:s"), Criteria::LESS_EQUAL);
      $c->add(OfferPeer::END_TIME, date("Y-m-d H:i:s"), Criteria::GREATER_THAN);
      $c->addDescendingOrderByColumn(OfferPeer::DISCOUNT_PERCENT);
      $offers = OfferPeer::doSelect($c);
      foreach($offers as $offer)
      {
        $url = $url_set->appendChild(new DOMElement('url'));
        $location = $url->appendChild(new DOMElement('loc', url_for($offer->getRouteName2().'?id='.$offer->getId().'&name='.Utils::slugify($offer->getName()), true)));
        $priority = $url->appendChild(new DOMElement('priority', '0.6'));
      }
      
      $sitemap->save(sfConfig::get('sf_web_dir').'/sitemap.xml');
    }
    
    public static function sendOffersEmail()
    {
      set_time_limit(0);
      
      $bodyHtml = '';
      $count = 0;
      $results = array();
      
      $c = new Criteria();
      $c->add(SitePeer::IS_ACTIVE, 1);
      $sites = SitePeer::doSelect($c);
      foreach($sites as $site)
      {
        $c = new Criteria();
        $c->add(OfferPeer::SITE_ID, $site->getId());
        $c->add(OfferPeer::ON_SITE, 1);
        $c->add(OfferPeer::START_TIME, date("Y-m-d H:i:s"), Criteria::LESS_EQUAL);
        $c->add(OfferPeer::END_TIME, date("Y-m-d H:i:s"), Criteria::GREATER_THAN);
        $offers = OfferPeer::doCount($c);      
        $results[$site->getName()] = $offers;
        
        $count += $offers;
      }
      
      arsort($results);
      foreach($results as $siteName => $result)
      {
        $bodyHtml .= '<b>'.$siteName.'</b> - '.$result.' oferte <br / >';
      }
      
      $bodyHtml = '<b>Total:</b> '.$count.' oferte<br /><br />'.$bodyHtml;

      $mailer = sfContext::getInstance()->getMailer();
      $message = $mailer->compose('website@reduceri-cadouri.ro', 'admin@reduceri-cadouri.ro', 'Cron SiteParser');
      $message->setBody($bodyHtml,'text/html');
      $mailer->send($message);
    }
    
    public static function publishShare()
    {
      set_time_limit(0);
      
      if(date('H') >= 7 && date('H') <= 18)
      {
        if($share = SocialSharePeer::getShareToPublish('facebook'))
        {
          $share->publish();
        }
        if($share = SocialSharePeer::getShareToPublish('twitter'))
        {
          $share->publish();
        }
      }
    }
    
    public static function indexOffers()
    {
      set_time_limit(0);

      $offers = OfferPeer::getForLuceneIndex();
      $indexed = 0;
      $errors = 0;
      
      foreach($offers as $offer)
      {
        try
        {
          $offer->updateLuceneIndex();
          $offer->setInSearch(true);
          $offer->save();
          
          $indexed++;
        }
        catch (Exception $e)
        {
          /*
          sfContext::getInstance()->getMailer()->composeAndSend(
            'website@reduceri-cadouri.ro',
            'admin@reduceri-cadouri.ro',
            'Index Offers Error',
            $e->getMessage()
          );
          */
          $errors++;
        }
      }
      if($errors)
      {
        sfContext::getInstance()->getMailer()->composeAndSend(
          'website@reduceri-cadouri.ro',
          'admin@reduceri-cadouri.ro',
          'Index Offers Errors',
          'Offers for index: '.count($offers).'. Indexed '.$indexed.' offers. Index errors: '.$errors
        );
      }
    }
  }